package com.foreveross.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.service.HrHouseInfoService;
import com.foreveross.service.LogService;
import com.foreveross.service.SysUserService;
import com.foreveross.util.AuthUtil;
import com.foreveross.util.ObjectToMap;
import com.foreveross.util.StringUtil;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.HrNewsInfo;
import com.foreveross.vo.ProductType;
import com.foreveross.vo.SysRole;
import com.foreveross.vo.common.FileVo;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;
import com.foreveross.vo.common.ResultVo;
import com.foreveross.SYWebService.vo.ConditionVo;
import com.foreveross.SYWebService.vo.DataVo;
import com.foreveross.SYWebService.vo.SYResult;
import com.foreveross.SYWebService.vo.SYSWin;
import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.common.Exception.SYHttpclientCallException;

/**
 * 楼盘信息
 * 
 * @author wm
 * 
 */
@Controller
@RequestMapping(value = "/sysHrHouseInfoContraller")
public class SysHrHouseInfoContraller extends BaseCostroller {
	private Logger log = Logger.getLogger(SysHrHouseInfoContraller.class);

	@Resource
	private HrHouseInfoService hrHouseInfoService;
	@Resource
	private SysUserService sysUserService;
	@Resource
	private LogService logService;

	/**
	 * 楼盘列表查询
	 * 
	 * @param request
	 * @param response
	 * @param name
	 * @param proType
	 * @param proLine
	 * @param OnePageCount
	 * @param currentPage
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getHouseList")
	public void getHouseList(HttpServletRequest request,
			HttpServletResponse response, String name, String proType,
			String province, String rows, String page,String userId) {
		ResultVo resultVo = new ResultVo();
		try {
			int countRecord = (int) hrHouseInfoService.getHouseListCount(name,
					proType, province,userId);
			PageVo<HrHouses> pageObj = new PageVo<HrHouses>(page, countRecord,
					rows);
			pageObj.setRows(hrHouseInfoService.getHouseList(name, proType,
					province, pageObj.getpageIndex(), pageObj.getpageSize(),userId));
			resultVo.setData(pageObj);
			resultVo.setSuccess(true);
			log.info("楼盘信息查询 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("楼盘信息查询 查看失败");
			log.error("楼盘信息查询 查看失败!", e);
		}
		List list = Arrays.asList("proType", "proLine","messageHouses","newsHouses","optimizedHouses");
		CommonUtil.PrintWriterResultWithJSON(response, resultVo, list);
	}
	
	/**
	 * 同步楼盘项目ID
	 * @param request
	 * @param response
	 * @param hrHousesId
	 * @param projectId
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/updateHrHousesProjectId")
	public void updateHrHousesProjectId(HttpServletRequest request,
			HttpServletResponse response, String hrHousesId, String projectId) {
		ResultVo resultVo = new ResultVo();
		try {
			hrHouseInfoService.updateHrHousesProjectId(hrHousesId, projectId);
			resultVo.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("楼盘项目ID同步失败！");
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	/**
	 * 获取思源项目ID
	 * @param request
	 * @param response
	 * @param cityName
	 * @param projectName  
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/synHrHouses")
	public void synHrHouses(HttpServletRequest request,
			HttpServletResponse response, String cityName, String projectName) {
		ResultVo resultVo = new ResultVo();
		try{
			ConditionVo conditionVo = new ConditionVo();
			conditionVo.setP0("UserRev_GetProject");
		   // conditionVo.setP1(cityName != null ? cityName : "");
			DataVo dataVo = hrHouseInfoService.synHrHouses(conditionVo, projectName);
			resultVo.setSuccess(true);
			resultVo.setData(dataVo);
			CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
		}catch(SYHttpclientCallException e){
			resultVo.setSuccess(false);
			resultVo.setMessage(e.getMessage());
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("联网认证失败!");
		}
	}
	
	/**
	 * 小区下拉列表获取
	 * @param request
	 * @param response
	 * @param id
	 * @param cityId
	 */

	@RequestMapping(method = RequestMethod.POST, value = "/getHrHousesSelect")
	public void getHrHousesSelect(HttpServletRequest request,
			HttpServletResponse response, String id, String cityId,String userId) {
		List list = new ArrayList();
		try {
			list = hrHouseInfoService.getHrHousesSelect(id, cityId,userId);
		} catch (Exception e) {
			log.error("列表信息获取失败！", e);
		}
		List listz = Arrays.asList("proType", "proLine","messageHouses","newsHouses","optimizedHouses");
		CommonUtil.PrintWriterResultVoWithJSON(response, list, listz);

	}
	
	/**
	 * 查询没有被物业管理的楼盘
	 * @param request
	 * @param response
	 * @param id
	 * @param cityId
	 * @param userId
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getHousesListWithOutTenementSelect")
	public void getHousesListWithOutTenementSelect(HttpServletRequest request,
			HttpServletResponse response, String id, String cityId,String userId) {
		List list = new ArrayList();
		try {
			list = hrHouseInfoService.getHousesListWithOutTenementSelect(id, cityId,userId);
		} catch (Exception e) {
			log.error("列表信息获取失败！", e);
		}
		List listz = Arrays.asList("proType", "proLine","messageHouses","newsHouses","optimizedHouses");
		CommonUtil.PrintWriterResultVoWithJSON(response, list, listz);

	}

	/**
	 * 楼盘信息更新
	 * 
	 * @param request
	 * @param response
	 * @param sysRole
	 * @param updateType
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/updateHouses")
	public void updateHouses(HttpServletRequest request,
			HttpServletResponse response, HrHouses hrHouses, String updateType ,String userId) {
		ResultVo resultVo = new ResultVo();
		try {
			
			hrHouseInfoService.saveOrUpdate(hrHouses, updateType);
			
			// 默认将权限授权给添加人员的角色
			List<SysRole> roles = sysUserService.getUserRoleList(userId);
			for (SysRole role : roles){
				sysUserService.userRoleBindHouse(role.getId(), hrHouses.getId());
			}
			
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_ADD,"楼盘信息" + Constants.getUpdateType(updateType),
					AuthUtil.getUserid(request),Constants.getUpdateType(updateType) + "楼盘信息["+ hrHouses.toString() + "]");
			log.info("楼盘信息 " + updateType + "成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("楼盘信息 " + updateType + "失败");
			log.error("楼盘信息 " + updateType + "失败!", e);
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}


	/**
	 * 
	 * 查看楼盘信息
	 * 
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/findHouses")
	public void findHouses(HttpServletRequest request,
			HttpServletResponse response, String id) {
		ResultVo resultVo = new ResultVo();
		try {
			HrHouses hrHouses = (HrHouses) this.findObject(HrHouses.class, id);
			List<FileVo> fileVoList = FileVo.getFileVoList(request, hrHouses.getUrl());
			resultVo.setOther(fileVoList);
			resultVo.setSuccess(true);
			resultVo.setData(hrHouses);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("查看楼盘信息失败");
		}
		// List list = Arrays.asList("proType", "proLine");
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 楼盘信息删除
	 * 
	 * @param request
	 * @param response
	 * @param ids
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/deleteHousesList")
	public void deleteHousesList(HttpServletRequest request,
			HttpServletResponse response, String ids) {
		ResultVo resultVo = new ResultVo();
		try {
			hrHouseInfoService.deleteHouses(ids);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_ADD, "楼盘信息删除",
					AuthUtil.getUserid(request), "楼盘信息[" + ids + "]");
			log.info("楼盘信息 删除成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage(Constants.SYS_BASEDATA_DEL_ERRE);
			log.info("楼盘信息 删除失败!");
			e.printStackTrace();
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

}
